﻿using EFCore.Models;
using LiveCharts.Wpf;
using LiveCharts;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows.Media.Media3D;
using Prism.Mvvm;
using Microsoft.EntityFrameworkCore.Query.Internal;
using System.Runtime.Serialization;

namespace HML
{
   public class MainViewModel: BindableBase
    {


        //今日入库
        private List<ViewMaterialWarehouseLine> materialWarehouseLines = new List<ViewMaterialWarehouseLine>();
        public List<ViewMaterialWarehouseLine> MaterialWarehouseLines
        {
            get { return materialWarehouseLines; }
            set { materialWarehouseLines = value; RaisePropertyChanged(); }
        }
        //今日出库
        private List<Lognote> lognotes;
        public List<Lognote> Lognotes
        {
            get { return lognotes; }
            set { lognotes = value; RaisePropertyChanged(); }
        }
        //信息汇总         
        private string rawMaterial;
        public string RawMaterial
        {
            get { return rawMaterial; }
            set { rawMaterial = value; RaisePropertyChanged(); ; }
        }//原材料
        private string incomplete;
        public string Incomplete
        {
            get { return incomplete; }
            set { incomplete = value; RaisePropertyChanged(); }
        }//未完成品
        private string surplus;
        public string Surplus
        {
            get { return surplus; }
            set { surplus = value; RaisePropertyChanged(); }
        }//剩余
        private string empty;
        public string Empty
        {
            get { return empty; }
            set { empty = value; RaisePropertyChanged(); }
        }//空箱
        private string taskcount;
        public string TaskCount
        {
            get { return taskcount; }
            set { taskcount = value; RaisePropertyChanged(); }
        }//任务统计
        private string retention;
        public  string Retention
        {
            get { return retention; }
            set { retention = value; RaisePropertyChanged(); }
        }//滞留任务

        //饼图
        List<string> titles = new List<string> { "Y", "W", "F", "P", "B" };

        private SeriesCollection pieSeriesCollection;
        public SeriesCollection PieSeriesCollection
        {
            get { return pieSeriesCollection; }
            set { pieSeriesCollection = value; RaisePropertyChanged(); }
        }
        //日期时间
        private string dateNow;
        public string DateNow
        {
            get { return dateNow; }
            set { dateNow = value;RaisePropertyChanged(); }
        }
        private string timenow;
        public string TimeNow
        {
            get { return timenow; }
            set { timenow = value;RaisePropertyChanged(); }
        }

        //柱状图
        private SeriesCollection seriesCollection;
        public SeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set { seriesCollection = value; RaisePropertyChanged(); }
        }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }

        //计时器
        private DispatcherTimer timer;
        private DispatcherTimer timer2;
        public MainViewModel()
        {    //饼图   
            PieSeriesCollection = new SeriesCollection();
        
            //定时器      
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(3); // 设置计时器的时间间隔为1秒
            timer.Tick += Timer_Tick; // 订阅计时器的Tick事件                             
            timer.Start(); // 启动计时器
                           // 定时器二
            timer2 = new DispatcherTimer();
            timer2.Interval = TimeSpan.FromDays(1); // 设置计时器的时间间隔为1秒
            timer2.Tick += Timer_Tick2; // 订阅计时器的Tick事件                             
            timer2.Start(); // 启动计时器
                            //柱状图
            getColoum();

        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            GetDb();
            GetdateTime();
        }
        private void Timer_Tick2(object sender, EventArgs e)
        {
             //柱状图
            getColoum();
     
        }

        private void GetDb()
        {
            using (MyDbContext db = new MyDbContext())
            {
                MaterialWarehouseLines = db.ViewMaterialWarehouseLines.Where(x => x.CreatedDate.Date == DateTime.Now.Date).ToList();
                MaterialWarehouseLines = MaterialWarehouseLines.OrderByDescending(x => x.CreatedDate).ToList();
                Lognotes = db.Lognotes.Where(x => x.Type == "出库" && x.DateTime.Date == DateTime.Now.Date).ToList();
                Lognotes = Lognotes.OrderByDescending(x => x.DateTime).ToList();
                //原材料
                RawMaterial = db.ViewMaterialWarehouseLines.Where(x => !x.MaterialName.StartsWith("B")).Select(x => x.MaterialName).Distinct().Count().ToString();

                //半成品
                Incomplete = db.ViewMaterialWarehouseLines.Where(x => x.MaterialName.StartsWith("B")).Select(x => x.MaterialName).Distinct().Count().ToString();
                //空箱
                var boxs = db.Boxes.Include(x => x.BoxGrids).ThenInclude(y => y.MaterialBases).Where(x => x.IdWarehouseLine != null).ToList();
                Empty = boxs.Where(x => x.BoxGrids.Any(y => y.MaterialBases.Count == 0)).Count().ToString();
                //任务
                TaskCount = db.Lognotes.Where(x => (x.DateTime.Date == DateTime.Now.Date) && (x.Type == "上架" || x.Type == "下架")).Count().ToString();
                //剩余库位
                Surplus = db.WarehouseLines.Include(x => x.Boxes).Where(x => x.Boxes.Count == 0).Count().ToString();
                //滞留任务
                Retention = db.TaskOrders.Where(x => x.State == "已下发" && x.CreatTime.Date == DateTime.Now.Date && DateTime.Now.Hour - x.CreatTime.Hour > 0.5).Count().ToString();
                //饼图
                if (PieSeriesCollection.Count == 0)
                {
                    foreach (var title in titles)
                    {
                        PieSeries series = new PieSeries();
                        double value = db.ViewMaterialWarehouseLines.Where(x => x.MaterialName.StartsWith(title)).Select(x => x.MaterialName).Distinct().Count();
                        ChartValues<double> chartvalue = new ChartValues<double>();
                        chartvalue.Add(value);
                        series.DataLabels = true;
                        series.Title = title + "料";
                        series.Values = chartvalue;
                        PieSeriesCollection.Add(series);
                    }
                }


            }
        }
        private  void GetdateTime()
        {
            DateNow=DateTime.Now.ToString("d");
            TimeNow = DateTime.Now.ToString("t");
        }
        private void getColoum()
        {

            //柱状图


            using (MyDbContext db = new MyDbContext())
            {

                //柱状图
                SeriesCollection = new SeriesCollection();
                ColumnSeries columnSeriesInTask = new ColumnSeries();
                ColumnSeries columnSeriesOutTask = new ColumnSeries();
                ChartValues<double> chartvalueInTask = new ChartValues<double>();
                ChartValues<double> chartvalueOutTask = new ChartValues<double>();
                columnSeriesInTask.Title = "上架";
                columnSeriesOutTask.Title = "下架";
                Labels = new string[10];
                for (int i = 0; i < 10; i++)
                {
                    DateTime startDate = DateTime.Today.AddDays(-(10-i)); // 获取当前日期往前数五天的日期
                    List<Lognote> InTask = db.Lognotes.Where(x => x.Type == "上架" && (x.DateTime.Date == startDate.Date)).ToList();
                    List<Lognote> OutTask = db.Lognotes.Where(x => x.Type == "下架" && (x.DateTime.Date == startDate.Date)).ToList();
                    Labels[i] = DateTime.Now.AddDays(-(10 - i)).ToString("d");
                    chartvalueInTask.Add(InTask.Count);
                    columnSeriesInTask.Values = chartvalueInTask;
                    chartvalueOutTask.Add(OutTask.Count);
                    columnSeriesOutTask.Values = chartvalueOutTask;
                }
                SeriesCollection.Add(columnSeriesInTask);
                SeriesCollection.Add(columnSeriesOutTask);
                Formatter = value => value.ToString("N");

            }
        }
    }
}
